package com.lbs.jiepiaisi.biz;

import android.content.Intent;

import com.lbs.jiepiaisi.bean.LocationBean;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.parser.LocationParser;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.HttpUtil;
import com.lbs.jiepiaisi.utils.UrlUtil;

import okhttp3.FormBody;
import okhttp3.Response;

/**
 *
 * 时间：2018/1/11 12:04
 * 作用：
 */

public class LocationBiz {
    //根据经纬度查询地址
    public void getAddRessWithLatAndLng(double lat,double lng,final String flag){
        //获得sp存储的部门id
        FormBody formBody = new FormBody
                .Builder()
                .add("lat",Double.toString(lat))//设置参数名称和参数值/*String.valueOf(deptId)*/
                .add("lng",Double.toString(lng))
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_SEARCH_ADDRESS_URL, formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {

                LocationBean locationBean= new LocationBean();
                try{
                    //返回结果
                    String jsonString = response.body().string();
                    locationBean= LocationParser.parser(jsonString);
                    locationBean.setStatus(Constant.STATUS_OK);
                }catch (Exception e){
                    locationBean.setStatus(Constant.STATUS_NETWORK_ERROR);
                    e.printStackTrace();
                    Intent intent = new Intent(flag);
                    intent.putExtra(Constant.KEY_DATA, locationBean);
                    App.instance.sendBroadcast(intent);
                }finally {
                    Intent intent = new Intent(flag);
                    intent.putExtra(Constant.KEY_DATA, locationBean);
                    App.instance.sendBroadcast(intent);
                }
            }
            @Override
            public void failed() {
                LocationBean locationBean=new LocationBean();
                locationBean.setStatus(Constant.STATUS_NO_NET);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, locationBean);
                App.instance.sendBroadcast(intent);
            }
            @Override
            public void noNetwork() {
                LocationBean locationBean=new LocationBean();
                locationBean.setStatus(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(Constant.ACCOUNT_LAT_LNG);
                intent.putExtra(Constant.KEY_DATA, locationBean);
                App.instance.sendBroadcast(intent);
            }
        });
    }
}
